Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsutils

Package Overview
Dependencies
Maintainers
1
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsutils

utilities for working with typescript's AST

  • 3.21.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26M
increased by3.52%
Maintainers
1
Weekly downloads
 
Created

What is tsutils?

The tsutils package provides a set of utility functions for working with TypeScript. It is particularly useful for developers building TypeScript tools and plugins, as it offers a variety of functions to manipulate and analyze TypeScript code.

What are tsutils's main functionalities?

Type Guards

This feature provides type guard functions to check the type of TypeScript nodes. In the example, `isVariableStatement` is used to check if a node is a variable statement.

const ts = require('typescript');
const tsutils = require('tsutils');

const node = ts.createSourceFile('example.ts', 'let x: number = 42;', ts.ScriptTarget.Latest);

if (tsutils.isVariableStatement(node.statements[0])) {
  console.log('This is a variable statement');
}

AST Utilities

AST (Abstract Syntax Tree) utilities help in traversing and manipulating the AST of TypeScript code. The example demonstrates how to find all variable declarations in a source file.

const ts = require('typescript');
const tsutils = require('tsutils');

const sourceFile = ts.createSourceFile('example.ts', 'let x: number = 42;', ts.ScriptTarget.Latest);

const variableDeclarations = tsutils.findVariableDeclarations(sourceFile);
console.log(variableDeclarations);

Type Utilities

Type utilities provide functions to work with TypeScript types. The example shows how to check if a variable's type is a number using `isTypeFlagSet`.

const ts = require('typescript');
const tsutils = require('tsutils');

const program = ts.createProgram(['example.ts'], {});
const checker = program.getTypeChecker();
const sourceFile = program.getSourceFile('example.ts');

sourceFile.forEachChild(node => {
  if (ts.isVariableDeclaration(node)) {
    const type = checker.getTypeAtLocation(node);
    console.log(tsutils.isTypeFlagSet(type, ts.TypeFlags.Number)); // true if the type is a number
  }
});

Other packages similar to tsutils

Keywords

FAQs

Package last updated on 07 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc